<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Fit Layout</title>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type"/>
    <script src="../../../scripts/jquery.min.js"></script>
    <script src="../../../scripts/miniui/miniui.js"></script>
    <link href="../../../scripts/miniui/themes/default/miniui.css" rel="stylesheet">
    <link href="../../../scripts/miniui/themes/icons.css" rel="stylesheet">
    <link href="../../../scripts/miniui/themes/pure/skin.css" rel="stylesheet">
    <script src="../../../scripts/core.js"></script>

</head>
<body>
<style type="text/css">
    html, body {
        margin: 0;
        padding: 0;
        border: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
    }
</style>
<div class="mini-toolbar" style="padding:2px;border-bottom:0;">
    <table style="width:100%;">
        <tr>
            <td style="width:100%;">
                <a class="mini-button" iconCls="icon-add" onclick="add()" plain="true">新加计费点</a>
                <a class="mini-button" iconCls="icon-edit" onclick="saveData()" plain="true">保存计费点</a>
                <a class="mini-button" iconCls="icon-close" onclick="delData()" plain="true">删除计费点</a>
                <a class="mini-button" iconCls="icon-upload" onclick="syn()" plain="true">同步配置</a>
            </td>
        </tr>
    </table>
</div>


<!--撑满页面-->
<div class="mini-fit">

    <mini-datagrid _dataField="data111" allowCellEdit="true" allowCellSelect="true"
                   allowResize="true"
                   class="mini-datagrid"
                   id="datagrid1"
                   idField="id"
                   multiSelect="false"
                   onselectionchanged="onSelectionChanged" pageSize="15" showSortIcon="true"
                   sortMode="client"
                   style="width:100%;height:100%;"
                   url="/cmsapi/getAllRechargeCfg">
        <columns>
            <column type="indexcolumn"></column>
            <column type="checkcolumn"></column>
            <column allowSort="true" field="id" headerAlign="center" name="id" width="150">
                id
            </column>
            <column allowSort="true" field="typeId" headerAlign="center" name="id" width="150">
                类型
            </column>
            <column field="consumeNum" width="100">充值金额
                <input class="mini-spinner" maxValue="100000000000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="param" width="100">参数
                <input class="mini-spinner" maxValue="10000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="callbackType" width="100">回调类型
                <input class="mini-spinner" maxValue="10000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="remark" width="100">备注
                <input class="mini-textbox" property="editor" style="width:100%;"/>
            </column>
            <column field="starTitle" width="100">星星标题
                <input class="mini-textbox" property="editor" style="width:100%;"/>
            </column>
            <column field="starDes" width="100">星星描述
                <input class="mini-textbox" property="editor" style="width:100%;"/>
            </column>
        </columns>
    </mini-datagrid>

</div>


<script type="text/javascript">

    var auth = GetParams()
    mini.parse();
    var grid = mini.get("datagrid1");
    var descGrid = mini.get("descGrid");
    grid.setAjaxOptions({
        headers: {"userId": auth.userId, "token": auth.token}
    });
    grid.load(null, function () {
        grid.select(0, true)
    }, function () {

    })


    function add() {

        mini.open({
            targetWindow: window,
            url: "AddRechargeCfg.html?userId=" + auth.userId + "&token=" + auth.token,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {

                grid.reload();
            }
        });
    }

    function onSelectionChanged(e, record, c) {

        var grid = e.sender;
        var record = grid.getSelected();
        console.log(record)
        if (record) {
            descGrid.load({
                lid: record.descId
            });

        } else {

        }


    }

    function addDescLang() {

        if (grid.getSelecteds().length == 0) return;
        var row = grid.getSelecteds()[0];

        mini.open({
            targetWindow: window,
            url: "AddTaskLangCfg.html?userId=" + auth.userId + "&token=" + auth.token + "&lid=" + row.descId,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {
                descGrid.load({
                    lid: row.descId
                });
            }
        });
    }


    function delDescData() {
        if (grid.getSelecteds().length == 0) {
            mini.alert("没有选中任何行")
            return;
        }
        if (descGrid.getSelecteds().length == 0) {
            mini.alert("没有选中任何行")
            return;
        }
        mini.confirm("确定删除记录？", "确定？",
            function (action) {
                if (action == "ok") {
                    var row = descGrid.getSelecteds()[0];
                    var task = grid.getSelecteds()[0];
                    var json = mini.encode(row);
                    descGrid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "/cmsapi/delLang",
                        data: json,
                        type: "post",
                        headers: {"userId": auth.userId, "token": auth.token},
                        contentType: 'application/json',
                        success: function (text) {
                            descGrid.load({
                                lid: task.descId
                            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                } else {

                }
            }
        );
    }

    function saveData() {
        if (grid.getSelecteds().length == 0) return;
        var row = grid.getSelecteds()[0];
        var json = mini.encode(row);
        grid.loading("保存中，请稍后......");
        $.ajax({
            url: "/cmsapi/editRechargeCfg",
            data: json,
            type: "post",
            contentType: 'application/json',
            headers: {"userId": auth.userId, "token": auth.token},
            success: function (text) {
                grid.reload();
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }

    async function syn() {
        mini.mask({
            el: document.body,
            cls: 'mini-mask-loading',
            html: '同步中...'
        });
        const tokenResp = await fetch("/sys/getTargetSysToken", {
            method: 'POST',
            headers: {"userId": auth.userId, "token": auth.token},
            contentType: 'application/json'
        });

        const sysResp = await tokenResp.json();

        for (var i = 0; i < grid.getSelecteds().length; i++) {

            var row = grid.getSelecteds()[i];
            var json = mini.encode(row);

            const response = await fetch(sysResp.url + "/cmsapi/editRechargeCfg",
                {
                    method: 'POST',
                    body: json,
                    headers: {
                        "userId": sysResp.loginResp.userId,
                        "token": sysResp.loginResp.token,
                        'Content-Type': 'application/json',
                    },
                });
            const resp = await response.json();
            console.log(resp)
        }
        mini.alert("同步成功");
        mini.unmask(document.body);
    }

    function delData() {
        if (grid.getSelecteds().length == 0) {
            alert("没有选中任何行")
            return;
        }
        mini.confirm("确定删除记录？", "确定？",
            function (action) {
                if (action == "ok") {
                    grid.loading("删除中，请稍后......");
                    var row = grid.getSelecteds()[0];
                    var json = mini.encode(row);
                    grid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "/cmsapi/delRechargeCfg",
                        data: json,
                        type: "post",
                        headers: {"userId": auth.userId, "token": auth.token},
                        contentType: 'application/json',
                        success: function (text) {
                            console.log("-----------")
                            grid.reload();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                } else {

                }
            }
        );
    }

</script>

</body>
</html>